#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
	DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null && pwd )"
	SOURCE="$(readlink "$SOURCE")"
	[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null && pwd )"

AFF3CT_PATH=$DIR

ls -lrt $AFF3CT_PATH/aff3ct-* > /dev/null 2>&1

if [ $? -gt 0 ]; then
	echo "There is no AFF3CT binary installed on the system (AFF3CT_PATH='$AFF3CT_PATH')."
	exit 1
fi

BIN=$(ls -lrt $AFF3CT_PATH/aff3ct-* | tail -n 1 | rev | cut -d" " -f1 | rev)

STR=$($BIN "$@" "-v" 2>&1)
if [[ $STR == *"mkl_intel"*".so: cannot open shared object file"* ]]; then
	source /opt/intel/vars-intel.sh
fi

#eval $BIN "$@"
( $BIN "$@" )